package com.crack.nga2.custom;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.Scroller;

import com.crack.nga2.R;
import com.crack.nga2.utils.GlobalConst;

/**
 * 侧滑finish
 * Created by lenovo on 2016/2/16.
 */
public class SlidingFinishLayout extends RelativeLayout {
    private static final float RATIO = 0.3f;
    public static int thresholdX = 100;
    public static int finishThresholdX = 300;
    public static int shadowWidth = 5;

    private ViewGroup viewParent;

    private int mTouchSlop;

    private int downX;

    private int downY;

    private int tempX;

    private Scroller mScroller;

    private boolean isSliding;

    private boolean isSlidable;

    private boolean isFinish;

    private int viewWidth;

    private Paint mShadowPaint;

    private Drawable mShapeDrawableLeft;
    private Drawable mShapeDrawableRight;

    public SlidingFinishLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
        mScroller = new Scroller(context);
        mShadowPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mShadowPaint.setColor(Color.argb(255, 0, 0, 0));
        mShapeDrawableLeft = context.getResources().getDrawable(R.drawable.shadow_left);
        mShapeDrawableRight = context.getResources().getDrawable(R.drawable.shadow_right);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        if (changed) {
            viewParent = this;
            viewWidth = this.getWidth();
            mShapeDrawableLeft.setBounds(0, 0, shadowWidth, getHeight());
            mShapeDrawableRight.setBounds(0, 0, shadowWidth, getHeight());
        }
    }


    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                float x = ev.getRawX();
                downX = (int) x;
                if (x < thresholdX || x > GlobalConst.SCREEN_WIDTH - thresholdX) {
                    isSlidable = true;
                }
                break;
            case MotionEvent.ACTION_MOVE:
                if (isSlidable) {
                    isSliding = true;
                    //拦截
                    return true;
                }
                break;
            case MotionEvent.ACTION_UP:
                isSlidable = false;
                break;
        }
        return super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_MOVE:
                tempX = (int) event.getRawX() - downX;
                if (isSliding) {
                    viewParent.scrollTo(-tempX, 0);
                    postInvalidate();
                }
                parentScrolling(viewParent.getScrollX());
                break;
            case MotionEvent.ACTION_UP:
                tempX = (int) (event.getRawX() - downX);
                if (isSliding) {
                    isSliding = false;
                    isSlidable = false;
                    if (Math.abs(tempX) < finishThresholdX) {
                        scrollOrigin();
                    } else {
                        isFinish = true;
                        scrollFinish();
                    }
                }
                break;
        }
        return super.onTouchEvent(event);
    }

    private void parentScrolling(int scrollX) {
        mScrollX = scrollX;
        mRatio = Math.abs(scrollX) * 1.0f / finishThresholdX;
        mShadowPaint.setAlpha((int) (180 * (1 - mRatio)));
    }

    private void scrollFinish() {
        int delta;
        delta = GlobalConst.SCREEN_WIDTH - viewParent.getScrollX() - 1;
        if (viewParent.getScrollX() < 0) {
            delta = -viewParent.getScrollX() - GlobalConst.SCREEN_WIDTH + 1;
        }
        mScroller.startScroll(viewParent.getScrollX(), 0, delta, 0, Math.abs(delta));
        postInvalidate();
    }

    private void scrollOrigin() {
        int delta = viewParent.getScrollX();
        mScroller.startScroll(delta, 0, -delta, 0, Math.abs(delta));
        postInvalidate();
    }

    @Override
    public void computeScroll() {
        super.computeScroll();
        if (mScroller.computeScrollOffset()) {
            viewParent.scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
            parentScrolling(viewParent.getScrollX());
            tempX = -viewParent.getScrollX();
            postInvalidate();
            if (mScroller.isFinished()) {
                if (isFinish) {
                    Activity activity = (Activity) getContext();
                    activity.finish();
                    activity.overridePendingTransition(0, 0);
                }
            }
        }
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        if (isSliding) {
            drawBackground(canvas);
        }
        drawShadow(canvas);
    }

    private void drawShadow(Canvas canvas) {
        if (Math.abs(mScrollX) > 1) {
            canvas.save();
            if (mScrollX < 0) {
                canvas.translate(-shadowWidth, 0);
                mShapeDrawableLeft.draw(canvas);
            } else {
                canvas.translate(getWidth(), 0);
                mShapeDrawableRight.draw(canvas);
            }
            canvas.restore();
        }
    }

    private void drawBackground(Canvas canvas) {
        if (Math.abs(mScrollX) >= finishThresholdX) {
            return;
        }
        if (mScrollX < 0) {
            canvas.drawRect(mScrollX, 0, 0, getHeight(), mShadowPaint);
        } else if (mScrollX > 0) {
            canvas.drawRect(getWidth(), 0, getWidth() + mScrollX, getHeight(), mShadowPaint);
        }
    }

    /**
     * 父布局的scrollX值
     */
    private int mScrollX;
    private float mRatio;

}
